package com.ruoyi.wms.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wms.domain.WmsInventoryMovementDetail;
import com.ruoyi.wms.mapper.WmsInventoryMovementMapper;
import com.ruoyi.wms.domain.WmsInventoryMovement;
import com.ruoyi.wms.service.IWmsInventoryMovementService;

/**
 * 库存移动Service业务层处理
 *
 * @author zmm
 * @date 2023-01-30
 */
@Service
public class WmsInventoryMovementServiceImpl implements IWmsInventoryMovementService
{
    @Autowired
    private WmsInventoryMovementMapper wmsInventoryMovementMapper;

    /**
     * 查询库存移动
     *
     * @param id 库存移动主键
     * @return 库存移动
     */
    @Override
    public WmsInventoryMovement selectWmsInventoryMovementById(Long id)
    {
        return wmsInventoryMovementMapper.selectWmsInventoryMovementById(id);
    }

    /**
     * 查询库存移动列表
     *
     * @param wmsInventoryMovement 库存移动
     * @return 库存移动
     */
    @Override
    public List<WmsInventoryMovement> selectWmsInventoryMovementList(WmsInventoryMovement wmsInventoryMovement)
    {
        return wmsInventoryMovementMapper.selectWmsInventoryMovementList(wmsInventoryMovement);
    }

    /**
     * 新增库存移动
     *
     * @param wmsInventoryMovement 库存移动
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsInventoryMovement(WmsInventoryMovement wmsInventoryMovement)
    {
        wmsInventoryMovement.setCreateTime(DateUtils.getNowDate());
        int rows = wmsInventoryMovementMapper.insertWmsInventoryMovement(wmsInventoryMovement);
        insertWmsInventoryMovementDetail(wmsInventoryMovement);
        return rows;
    }

    /**
     * 修改库存移动
     *
     * @param wmsInventoryMovement 库存移动
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsInventoryMovement(WmsInventoryMovement wmsInventoryMovement)
    {
        wmsInventoryMovement.setUpdateTime(DateUtils.getNowDate());
        wmsInventoryMovementMapper.deleteWmsInventoryMovementDetailByInventoryMovementId(wmsInventoryMovement.getId());
        insertWmsInventoryMovementDetail(wmsInventoryMovement);
        return wmsInventoryMovementMapper.updateWmsInventoryMovement(wmsInventoryMovement);
    }

    /**
     * 批量删除库存移动
     *
     * @param ids 需要删除的库存移动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsInventoryMovementByIds(Long[] ids)
    {
        wmsInventoryMovementMapper.deleteWmsInventoryMovementDetailByInventoryMovementIds(ids);
        return wmsInventoryMovementMapper.deleteWmsInventoryMovementByIds(ids);
    }

    /**
     * 删除库存移动信息
     *
     * @param id 库存移动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsInventoryMovementById(Long id)
    {
        wmsInventoryMovementMapper.deleteWmsInventoryMovementDetailByInventoryMovementId(id);
        return wmsInventoryMovementMapper.deleteWmsInventoryMovementById(id);
    }

    /**
     * 新增库存移动详情信息
     *
     * @param wmsInventoryMovement 库存移动对象
     */
    public void insertWmsInventoryMovementDetail(WmsInventoryMovement wmsInventoryMovement)
    {
        List<WmsInventoryMovementDetail> wmsInventoryMovementDetailList = wmsInventoryMovement.getWmsInventoryMovementDetailList();
        Long id = wmsInventoryMovement.getId();
        if (StringUtils.isNotNull(wmsInventoryMovementDetailList))
        {
            List<WmsInventoryMovementDetail> list = new ArrayList<WmsInventoryMovementDetail>();
            for (WmsInventoryMovementDetail wmsInventoryMovementDetail : wmsInventoryMovementDetailList)
            {
                wmsInventoryMovementDetail.setInventoryMovementId(id);
                wmsInventoryMovementDetail.setCreateTime(wmsInventoryMovement.getCreateTime());
                wmsInventoryMovementDetail.setCreateBy(wmsInventoryMovement.getCreateBy());
                wmsInventoryMovementDetail.setDelFlag(0);
                list.add(wmsInventoryMovementDetail);
            }
            if (list.size() > 0)
            {
                wmsInventoryMovementMapper.batchWmsInventoryMovementDetail(list);
            }
        }
    }
}
